<?php

namespace App\Utils\Base;

use Doctrine\ORM\QueryBuilder;
use Symfony\Bundle\MakerBundle\Str;

class FilterBase {

    /** @var string sql 查询别名 */
    const ALIAS = 'o';
    /** @var QueryBuilder */
    public $builder;
    /** @var array params */
    public $params;

    public function __construct (array $params = []) {
        $this->params = $params;
    }

    /**
     * @param QueryBuilder $builder
     * @return QueryBuilder
     */
    public function apply (QueryBuilder $builder): QueryBuilder {
        $this->builder = $builder;
        foreach ($this->params as $key => $value) {
            $method = Str::asLowerCamelCase($key);
            if (method_exists($this, $method) && !is_null($value)) {
                $this->$method(self::ALIAS.'.'.$key, $value);
            }
        }
        return $this->builder;
    }
}